<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>课时31_数据访问对象模式</title>
</head>

<body>
    <div class="course-content">
        <div class="current-component">
            <div class="text markdown-body">
                <div>
                    <h1
                        style="border:0px;margin:0px 0px 10px;padding:0px;font-size:2.1em;font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        数据访问对象模式</h1>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        数据访问对象模式（Data Access Object Pattern）或 DAO 模式用于把低级的数据访问 API 或操作从高级的业务服务中分离出来。以下是数据访问对象模式的参与者。</p>

                    <ul class="list list-paddingleft-2" style="list-style-type:none;">
                        <li>
                            <p><strong>数据访问对象接口（Data Access Object Interface）</strong>&nbsp;- 该接口定义了在一个模型对象上要执行的标准操作。
                            </p>
                        </li>
                        <li>
                            <p><strong>数据访问对象实体类（Data Access Object concrete class）</strong>&nbsp;-
                                该类实现了上述的接口。该类负责从数据源获取数据，数据源可以是数据库，也可以是 xml，或者是其他的存储机制。</p>
                        </li>
                        <li>
                            <p><strong>模型对象/数值对象（Model Object/Value Object）</strong>&nbsp;- 该对象是简单的 POJO，包含了 get/set
                                方法来存储通过使用 DAO 类检索到的数据。</p>
                        </li>
                    </ul>
                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        实现</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        我们将创建一个作为模型对象或数值对象的&nbsp;<em>Student</em>&nbsp;对象。<em>StudentDao</em>&nbsp;是数据访问对象接口。<em>StudentDaoImpl</em>&nbsp;是实现了数据访问对象接口的实体类。<em>DaoPatternDemo</em>，我们的演示类使用&nbsp;<em>StudentDao</em>&nbsp;来演示数据访问对象模式的用法。
                    </p>

                    <p><img alt="" src="https://developer.aliyun.com/files/course/2017/09-24/132103fe3895212644.jpg">
                    </p>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 1</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        创建数值对象。</p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>Student.java</em>
                    </p>

                    <pre><code class="language-java">public class Student {
   private String name;
   private int rollNo;

   Student(String name, int rollNo){
      this.name = name;
      this.rollNo = rollNo;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public int getRollNo() {
      return rollNo;
   }

   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }}</code></pre>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 2</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        创建数据访问对象接口。</p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>StudentDao.java</em>
                    </p>

                    <pre><code class="language-java">import java.util.List;public interface StudentDao {
   public List<student> getAllStudents();
   public Student getStudent(int rollNo);
   public void updateStudent(Student student);
   public void deleteStudent(Student student);}</student></code></pre>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 3</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        创建实现了上述接口的实体类。</p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>StudentDaoImpl.java</em>
                    </p>

                    <pre><code class="language-java">import java.util.ArrayList;import java.util.List;public class StudentDaoImpl implements StudentDao {
	
   //列表是当作一个数据库
   List<student> students;

   public StudentDaoImpl(){
      students = new ArrayList<student>();
      Student student1 = new Student("Robert",0);
      Student student2 = new Student("John",1);
      students.add(student1);
      students.add(student2);		
   }
   @Override
   public void deleteStudent(Student student) {
      students.remove(student.getRollNo());
      System.out.println("Student: Roll No " + student.getRollNo() 
         +", deleted from database");
   }

   //从数据库中检索学生名单
   @Override
   public List<student> getAllStudents() {
      return students;
   }

   @Override
   public Student getStudent(int rollNo) {
      return students.get(rollNo);
   }

   @Override
   public void updateStudent(Student student) {
      students.get(student.getRollNo()).setName(student.getName());
      System.out.println("Student: Roll No " + student.getRollNo() 
         +", updated in the database");
   }}</student></student></student></code></pre>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 4</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        使用&nbsp;<em>StudentDao</em>&nbsp;来演示数据访问对象模式的用法。</p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>CompositeEntityPatternDemo.java</em>
                    </p>

                    <pre><code class="language-java">public class DaoPatternDemo {
   public static void main(String[] args) {
      StudentDao studentDao = new StudentDaoImpl();

      //输出所有的学生
      for (Student student : studentDao.getAllStudents()) {
         System.out.println("Student: [RollNo : "
            +student.getRollNo()+", Name : "+student.getName()+" ]");
      }


      //更新学生
      Student student =studentDao.getAllStudents().get(0);
      student.setName("Michael");
      studentDao.updateStudent(student);

      //获取学生
      studentDao.getStudent(0);
      System.out.println("Student: [RollNo : "
         +student.getRollNo()+", Name : "+student.getName()+" ]");		
   }}</code></pre>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 5</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        验证输出。</p>

                    <pre><code class="language-java">Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]</code></pre>

                    <p>&nbsp;</p>
                </div>
            </div>
        </div>
    </div>
</body>

</html>